package labeledbox;

import java.awt.*;
import java.awt.geom.*;



class LabeledBox implements Shape {

  RectShape box   = new RectShape();
  TextShape label = new TextShape();

  // flag to let draw routine know whether it needs to re-layout the components of the labeled box. 
  // The labeled box must be laid out when it is first created, hence we initialize damaged to true.
  boolean damaged = true;

  // constants
  public static final Color DEFAULT_TEXT_COLOR = Color.yellow;
  public static final Color DEFAULT_TEXT_FILL_COLOR = Color.black;
  public static final Color DEFAULT_BOX_BORDER_COLOR = Color.black;
  static final int MARGIN = 20; // 20 pixel margin

  // Selected labeled box defaults
  public static final Color DEF_SELECTED_TEXT_COLOR       = Color.white;
  public static final Color DEF_SELECTED_BOX_BORDER_COLOR = Color.blue;
  public static final boolean DEF_SELECTED_BOX_FILL_BOOL  = false;

  final static int FONT_SIZE_SMALL = 8;
  final static int FONT_SIZE_MED   = 12;
  final static int FONT_SIZE_LARGE = 24;

  // Constructor
  public LabeledBox(int left, int top, Color backgroundColor, String text) {

    label.setLabel(text);
    label.setFillColor(DEFAULT_TEXT_FILL_COLOR);
    label.setLineColor(DEFAULT_TEXT_COLOR);

    box.setLeft(left);
    box.setTop(top);
    box.setFillColor(backgroundColor);
    box.setLineColor(DEFAULT_BOX_BORDER_COLOR);
    box.setWidth( label.getWidth()  + 2*MARGIN);
    box.setHeight(label.getHeight() + 2*MARGIN);
  } // end constructor


  // DRAW() 
  public void draw(Graphics g) {
    if (damaged) {
	    // update layout--the text label should be centered
	    // in the box with a 20 pixel margin and the box should
	    // be just large enough to accommodate the label with
	    // a 20 pixel margin
	    label.setLeft(box.getLeft() + MARGIN);
	    label.setTop( box.getTop()  + MARGIN);
	    box.setWidth( label.getWidth()  + 2*MARGIN);
	    box.setHeight(label.getHeight() + 2*MARGIN);
	    damaged = false;
    }
    box.draw(g);
    label.draw(g);
  } ///// end draw() ////////

  
  // CONTAINS()
  public boolean contains(int x, int y) {
    return box.contains(x,y);
  }


  ///////////////////////////////////////
  //// GETTERS & SETTERS ////////////////

  public void setLabel(String l) {
    label.setLabel(l);
    damaged = true;
  }

  public Font getFont() {
    return label.getFont();
  }

  public void setFont(Font f) {
    label.setFont(f);
    damaged = true;
  }

  public void setLeft(int left) {
    box.setLeft(left);
    damaged = true;
  }

  public void setTop(int top) {
    box.setTop(top);
    damaged = true;
  }

  public int getLeft() {
    return box.getLeft();
  }

  public int getTop() {
    return box.getTop();
  }

  public int getWidth() {
    return box.getWidth();
  }

  public int getHeight() {
    return box.getHeight();
  }

  public Color getFillColor() {
    return box.getFillColor();
  }

  public void setFillColor(Color newColor) {
    box.setFillColor(newColor);
  }

  public Color getLineColor() {
    return box.getLineColor();
  }

  public void setLineColor(Color newColor) {
    box.setLineColor(newColor);
  }

  public Color getTextColor() {
    return label.getLineColor();
  }

  public void setTextColor(Color newColor) {
    label.setLineColor(newColor);
  }

  public boolean getFilled() {
    return box.getFilled();
  }

  public void setFilled(boolean isFilled) {
    box.setFilled(isFilled);
  }

} //////////////////////////////// end class /////////////////////////////////////

    
